import bb.cascades 1.0
import bb.data 1.0
import bb.system 1.0
import com.gigigo.utils 1.0
import com.gigigo.core 1.0

Page {
    id: lastGameDetailPageRoot
    property variant colorText_gray: Color.create("#414141")
    property variant colorText_black: Color.create("#000000")
    property variant colorText_white: Color.create("#ffffff")
    property bool isLoading: true
    property int segmentedType: 1
    property variant detailObject

    signal videosClicked(variant _videosObject)

    Container {
        horizontalAlignment: HorizontalAlignment.Fill
        verticalAlignment: VerticalAlignment.Fill
        layout: DockLayout {
        }
        attachedObjects: [
            ImagePaintDefinition {
                id: backgroundBg
                repeatPattern: RepeatPattern.XY
                imageSource: "asset:///images/background.amd"
            }
        ]
        background: backgroundBg.imagePaint
        Container {
            layout: StackLayout {
            }
            Container {
                id: header
                minHeight: 144
                maxHeight: 144
                horizontalAlignment: HorizontalAlignment.Fill
                background: headerBg.imagePaint
                layout: DockLayout {
                }
                Container {
                    horizontalAlignment: HorizontalAlignment.Left
                    verticalAlignment: VerticalAlignment.Center
                    translationX: 25
                    layout: StackLayout {
                        orientation: LayoutOrientation.LeftToRight
                    }
                    ImageView {
                        imageSource: "asset:///images/flag1.png"
                        horizontalAlignment: HorizontalAlignment.Left
                        verticalAlignment: VerticalAlignment.Center
                    }
                    Label {
                        id: teamLabel
                        text: DB.getAppName()
                        horizontalAlignment: HorizontalAlignment.Right
                        verticalAlignment: VerticalAlignment.Center
                        textStyle.color: colorText_white
                        textStyle.fontSize: FontSize.XLarge
                        textStyle.fontWeight: FontWeight.Bold
                    }
                }
                ImageView {
                    imageSource: "asset:///images/logo.png"
                    horizontalAlignment: HorizontalAlignment.Right
                    verticalAlignment: VerticalAlignment.Center
                    translationX: -20
                    translationY: 10
                }
                attachedObjects: [
                    ImagePaintDefinition {
                        id: headerBg
                        repeatPattern: RepeatPattern.X
                        imageSource: "asset:///images/Header.png"
                    }
                ]
            }
            Container {
                id: subheader
                minHeight: 97
                maxHeight: 97
                horizontalAlignment: HorizontalAlignment.Fill
                translationY: -3
                background: subheaderBg.imagePaint
                Container {
                    preferredHeight: 97
                    verticalAlignment: VerticalAlignment.Fill
                    horizontalAlignment: HorizontalAlignment.Left
                    leftPadding: 25
                    layout: StackLayout {
                        orientation: LayoutOrientation.LeftToRight
                    }
                    Label {
                        id: titleText
                        text: qsTr("Último Jogo")
                        verticalAlignment: VerticalAlignment.Center
                        textStyle.color: colorText_white
                        textStyle.fontSize: FontSize.Large
                        textStyle.fontWeight: FontWeight.Bold
                    }
                }
                attachedObjects: [
                    ImagePaintDefinition {
                        id: subheaderBg
                        repeatPattern: RepeatPattern.X
                        imageSource: "asset:///images/title_header.png"
                    }
                ]
            }
            Container {
                id: bodyContent
                horizontalAlignment: HorizontalAlignment.Fill
                verticalAlignment: VerticalAlignment.Fill
                layout: StackLayout {
                }
                topPadding: 20
                bottomPadding: 5

                Container {
                    horizontalAlignment: HorizontalAlignment.Fill
                    leftPadding: 15
                    rightPadding: 15

                    Container {
                        id: itemLastGame
                        horizontalAlignment: HorizontalAlignment.Fill

                        topPadding: 25
                        bottomPadding: 25
                        leftPadding: 25
                        rightPadding: 20
                        background: slideBg.imagePaint
                        layout: DockLayout {
                        }
                        attachedObjects: [
                            ImagePaintDefinition {
                                id: slideBg
                                imageSource: "asset:///images/slide.amd"
                            }
                        ]
                        Container {
                            preferredHeight: 200
                            leftPadding: 10
                            rightPadding: 20
                            horizontalAlignment: HorizontalAlignment.Fill
                            verticalAlignment: VerticalAlignment.Fill
                            layout: DockLayout {
                            }
                            Container {
                                horizontalAlignment: HorizontalAlignment.Fill
                                verticalAlignment: VerticalAlignment.Top
                                translationY: -10
                                Label {
                                    id: lastDescriptionLabel
                                    horizontalAlignment: HorizontalAlignment.Center
                                    textStyle.color: colorText_gray
                                    textStyle.fontSize: FontSize.Small
                                }
                            }
                            Container {
                                leftPadding: 80
                                rightPadding: 80
                                horizontalAlignment: HorizontalAlignment.Fill
                                verticalAlignment: VerticalAlignment.Center
                                layout: DockLayout {
                                }
                                Container {
                                    horizontalAlignment: HorizontalAlignment.Left
                                    verticalAlignment: VerticalAlignment.Center
                                    layout: StackLayout {
                                        orientation: LayoutOrientation.LeftToRight
                                    }
                                    layoutProperties: StackLayoutProperties {
                                        spaceQuota: 5
                                    }
                                    Label {
                                        id: lastGameTeam1Label
                                        horizontalAlignment: HorizontalAlignment.Left
                                        verticalAlignment: VerticalAlignment.Center
                                        textStyle.color: colorText_black
                                        textStyle.fontSize: FontSize.Medium
                                        textStyle.fontWeight: FontWeight.Bold
                                    }
                                    /*
                                     * Commons.GGGImage {
                                     * id: nextGameTeam1Img
                                     * verticalAlignment: VerticalAlignment.Center
                                     * horizontalAlignment: HorizontalAlignment.Center
                                     * preferredWidth: 225
                                     * preferredHeight: 140
                                     * sourceUrl: loadImage()
                                     * }
                                     */
                                    ImageView {
                                        id: lastGameTeam1Img
                                        imageSource: "asset:///images/flag1.png"
                                        horizontalAlignment: HorizontalAlignment.Center
                                        verticalAlignment: VerticalAlignment.Center
                                        preferredWidth: 57
                                        preferredHeight: 71
                                    }
                                    Label {
                                        id: lastGameTeam1Score
                                        horizontalAlignment: HorizontalAlignment.Right
                                        verticalAlignment: VerticalAlignment.Center
                                        textStyle.color: colorText_black
                                        textStyle.fontSize: FontSize.Medium
                                        textStyle.fontWeight: FontWeight.Bold
                                    }
                                }
                                Container {
                                    horizontalAlignment: HorizontalAlignment.Center
                                    verticalAlignment: VerticalAlignment.Center
                                    layoutProperties: StackLayoutProperties {
                                        spaceQuota: 1
                                    }
                                    Label {
                                        text: "x"
                                        horizontalAlignment: HorizontalAlignment.Center
                                        verticalAlignment: VerticalAlignment.Center
                                        textStyle.color: colorText_gray
                                        textStyle.fontSize: FontSize.Small
                                    }
                                }
                                Container {
                                    horizontalAlignment: HorizontalAlignment.Right
                                    verticalAlignment: VerticalAlignment.Center
                                    layout: StackLayout {
                                        orientation: LayoutOrientation.LeftToRight
                                    }
                                    layoutProperties: StackLayoutProperties {
                                        spaceQuota: 5
                                    }
                                    Label {
                                        id: lastGameTeam2Score
                                        horizontalAlignment: HorizontalAlignment.Left
                                        verticalAlignment: VerticalAlignment.Center
                                        textStyle.color: colorText_black
                                        textStyle.fontSize: FontSize.Medium
                                        textStyle.fontWeight: FontWeight.Bold
                                    }
                                    /*
                                     * Commons.GGGImage {
                                     * id: lastGameTeam2Img
                                     * verticalAlignment: VerticalAlignment.Center
                                     * horizontalAlignment: HorizontalAlignment.Center
                                     * preferredWidth: 225
                                     * preferredHeight: 140
                                     * sourceUrl: loadImage()
                                     * }
                                     */
                                    ImageView {
                                        id: lastGameTeam2Img
                                        imageSource: "asset:///images/flag2.png"
                                        horizontalAlignment: HorizontalAlignment.Center
                                        verticalAlignment: VerticalAlignment.Center
                                        preferredWidth: 57
                                        preferredHeight: 71
                                    }
                                    Label {
                                        id: lastGameTeam2Label
                                        horizontalAlignment: HorizontalAlignment.Right
                                        verticalAlignment: VerticalAlignment.Center
                                        textStyle.color: colorText_black
                                        textStyle.fontSize: FontSize.Medium
                                        textStyle.fontWeight: FontWeight.Bold
                                    }
                                }
                            }
                            Container {
                                horizontalAlignment: HorizontalAlignment.Fill
                                verticalAlignment: VerticalAlignment.Bottom
                                Label {
                                    id: lastDateLabel
                                    horizontalAlignment: HorizontalAlignment.Center
                                    textStyle.color: colorText_gray
                                    textStyle.fontSize: FontSize.Small
                                }
                            }
                        }
                    }
                }
                Container {
                    id: segmented
                    topMargin: 20
                    horizontalAlignment: HorizontalAlignment.Fill
                    verticalAlignment: VerticalAlignment.Center
                    background: segmentedBg.imagePaint
                    SegmentedControl {
                        id: segmentedControl
                        verticalAlignment: VerticalAlignment.Center
                        Option {
                            id: matchOption
                            text: "Lances"
                            value: 0
                            selected: true
                        }
                        Option {
                            id: videosOption
                            text: "Vídeos"
                            value: 1
                            selected: false
                        }
                        Option {
                            id: selectionOption
                            text: "Escalaçao"
                            value: 2
                            selected: false
                        }
                        Option {
                            id: statsOption
                            text: "Estatísticas"
                            value: 3
                            selected: false
                        }
                        onSelectedIndexChanged: {
                            lastGameDetailPageRoot.segmentedType = segmentedControl.selectedValue;
                            if (lastGameDetailPageRoot.segmentedType == 0) {
                                loadHighlightsContent();
                            } else if (lastGameDetailPageRoot.segmentedType == 1) {
                                loadVideosContent();
                            } else if (lastGameDetailPageRoot.segmentedType == 2) {
                                loadSelectionContent();
                            } else if (lastGameDetailPageRoot.segmentedType == 3) {
                                loadStatsContent();
                            }
                        }
                    }
                    attachedObjects: [
                        ImagePaintDefinition {
                            id: segmentedBg
                            repeatPattern: RepeatPattern.X
                            imageSource: "asset:///images/segmented_header.png"
                        }
                    ]
                }
                Container {
                    id: nextListContent
                    horizontalAlignment: HorizontalAlignment.Fill
                    verticalAlignment: VerticalAlignment.Fill
                    topPadding: 20
                    bottomPadding: 5
                    leftPadding: 15
                    rightPadding: 15

                    attachedObjects: [
                        ComponentDefinition {
                            id: highlightsContainerDefinition
                            source: "HighlightsListContainer.qml"
                        },
                        ComponentDefinition {
                            id: videosContainerDefinition
                            source: "VideosListContainer.qml"
                        },
                        ComponentDefinition {
                            id: selectionContainerDefinition
                            source: "SelectionListContainer.qml"
                        },
                        ComponentDefinition {
                            id: statsContainerDefinition
                            source: "StatsListContainer.qml"
                        }
                    ]
                }
            }
        }
    }
    actions: [
        ActionItem {
            id: updateAction
            title: qsTr("Actualizar")
            imageSource: "asset:///images/menuBar/AppMenuRefreshIcon.png"
            ActionBar.placement: ActionBarPlacement.OnBar
            enabled: ! isLoading
            onTriggered: {
                //loadContent();
            }
        }
    ]
    onDetailObjectChanged: {
        
        console.log("data: " + detailObject.data);
        var dateAux_split = detailObject.data.split("-");
        var day_split = (dateAux_split[2]).split("T");
        var dateAux = new Date(detailObject.date);
        
        lastDescriptionLabel.setText(detailObject.campeonato.nome + " - " + detailObject.rodada + "ª Rodada.");
        if (detailObject.equipe_visitante) {
            var visitante = detailObject.equipe_visitante.nome;
            visitante = visitante.substring(0, 3);
            lastGameTeam1Label.setText(visitante);
            //nextGameTeam1Img.setText(detailObject.);
            lastGameTeam1Score.setText(detailObject.placar_visitante);
        }
        if (detailObject.equipe_mandante) {
            var local = detailObject.equipe_mandante.nome;
            local = local.substring(0, 3);
            lastGameTeam2Label.setText(local);
            //nextGameTeam2Img.setText(detailObject.);
            lastGameTeam2Score.setText(detailObject.placar_mandante);
        }
        // if (detailObject.status== 0) {
        lastDateLabel.setText(detailObject.status + " - " + detailObject.estadio.nome);
        // } else if (detailObject.status == 1) {
        //nextDateLabel.setText(day_split[1] + " - " + detailObject.stadium);
        //}s
        loadHighlightsContent();
    }

    function loadHighlightsContent() {
        var highlightsDataContent = highlightsContainerDefinition.createObject();
        var _dataList = [
            {
                description: "Golos marcados",
                team1: "15",
                team2: "16"
            },
            {
                description: "Golos sofridos",
                team1: "9",
                team2: "8"
            },
            {
                description: "Passe certo",
                team1: "2462",
                team2: "1986"
            },
            {
                description: "Passe errado",
                team1: "253",
                team2: "190"
            },
            {
                description: "Finalizaçao certa",
                team1: "48",
                team2: "51"
            },
            {
                description: "Finalizaçao errada",
                team1: "30",
                team2: "21"
            } ];
        highlightsDataContent.dataList = _dataList;
        nextListContent.removeAll();
        nextListContent.add(highlightsDataContent);
    }
    function loadVideosContent() {
        var videosDataContent = videosContainerDefinition.createObject();
        var _dataList = [
            {
                id: "1",
                title: "Vasco 1x0 Palmeiras pela 11ª rodada do Brasileirão 2011",
                description: "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                date: "20/07/11",
                hour: "21:03",
                time: "03:13"
            },
            {
                id: "2",
                title: "Flamingo 1x0 Botafogo pela 11ª rodada do Brasileirão 2011 rodada do Brasileirão 2011 rodada do Brasileirão 2011",
                description: "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                date: "20/07/11",
                hour: "21:04",
                time: "03:13"
            },
            {
                id: "3",
                title: "Santos 1x0 Fluminense pela 11ª rodada do Brasileirão 2011 rodada do Brasileirão 2011",
                description: "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                date: "20/07/11",
                hour: "21:05",
                time: "03:13"
            },
            {
                id: "4",
                title: "Vasco 1x0 Fluminense pela 11ª rodada do Brasileirão 2011",
                description: "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                date: "20/07/11",
                hour: "21:06",
                time: "03:13"
            },
            {
                id: "5",
                title: "Vasco 1x0 Flamingo pela 11ª rodada do Brasileirão 2011 rodada do Brasileirão 2011",
                description: "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                date: "20/07/11",
                hour: "21:12",
                time: "03:13"
            },
            {
                id: "6",
                title: "Fluminense 1x0 Botafogo pela 11ª rodada do Brasileirão 2011",
                description: "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                date: "20/07/11",
                hour: "21:15",
                time: "03:13"
            },
            {
                id: "7",
                title: "Santos 1x0 Palmeiras pela 11ª rodada do Brasileirão 2011 rodada do Brasileirão 2011 rodada do Brasileirão 2011",
                description: "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                date: "20/07/11",
                hour: "21:16",
                time: "03:13"
            },
            {
                id: "8",
                title: "Vasco 1x0 Santos pela 11ª rodada do Brasileirão 2011",
                description: "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                date: "20/07/11",
                hour: "21:56",
                time: "03:13"
            },
            {
                id: "9",
                title: "Botafogo 1x0 Flamingo pela 11ª rodada do Brasileirão 2011 rodada do Brasileirão 2011",
                description: "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                date: "20/07/11",
                hour: "21:57",
                time: "03:13"
            },
            {
                id: "10",
                title: "Vasco 1x0 Fluminense pela 11ª rodada do Brasileirão 2011",
                description: "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                date: "20/07/11",
                hour: "21:59",
                time: "03:13"
            } ];
        videosDataContent.dataList = _dataList;
        nextListContent.removeAll();
        nextListContent.add(videosDataContent);
        videosDataContent.clicked.connect(gotoVideoDetail);
    }
    function loadSelectionContent() {
        var selectionDataContent = selectionContainerDefinition.createObject();
        var _dataList = [

            {
                name: "Danilo Fernandes",
                number: "22",
                status: "in"
            },
            {
                name: "Alessandro",
                number: "2",
                status: ""
            },
            {
                name: "Romarinho",
                number: "31",
                status: "out"
            },
            {
                name: "Danilo Fernandes",
                number: "22",
                status: "in"
            },
            {
                name: "Alessandro",
                number: "2",
                status: ""
            } ];
        selectionDataContent.dataList = _dataList;
        nextListContent.removeAll();
        nextListContent.add(selectionDataContent);
    }
    function loadStatsContent() {
        var statsDataContent = statsContainerDefinition.createObject();
        var _dataList = [
            {
                description: "Golos marcados",
                team1: "15",
                team2: "16"
            },
            {
                description: "Golos sofridos",
                team1: "9",
                team2: "8"
            },
            {
                description: "Passe certo",
                team1: "2462",
                team2: "1986"
            },
            {
                description: "Passe errado",
                team1: "253",
                team2: "190"
            },
            {
                description: "Finalizaçao certa",
                team1: "48",
                team2: "51"
            },
            {
                description: "Finalizaçao errada",
                team1: "30",
                team2: "21"
            } ];

        var estadisticas = new XMLHttpRequest();
        estadisticas.onreadystatechange = function() {
            if (estadisticas.readyState == 4) {
                console.log("estadisticas: " + estadisticas.responseText);
                eval("var ListEstadisticas = " + estadisticas.responseText)

            } else {
                console.log("Error al leer las estadisticas");
            }
        }
        estadisticas.open("GET", "https://api.futebol.globosat.tv/futebol/partida/?format=json", true);
        estadisticas.setRequestHeader("Authorization", "Token " + DB.getAuthToken());
        estadisticas.send(null);
        
        statsDataContent.dataList = _dataList;
        nextListContent.removeAll();
        nextListContent.add(statsDataContent);
        statsDataContent.clicked.connect(gotoStatsDetail);
    }

    function gotoVideoDetail(_videosObject) {
        console.debug("LastGameDetailPage - gotoVideoDetail! " + JSON.stringify(_videosObject));
        videosClicked(_videosObject);
    }
    function gotoStatsDetail(_statsObject) {

    }
}
